<template>
  <div class="main">
    <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
      <!--      <a-tabs :active-key="customActiveKey" :tab-bar-style="{ textAlign: 'center', borderBottom: 'unset' }" @change="handleTabClick">-->
      <!--        <a-tab-pane key="tab1" tab="账号密码登录">-->
      <div class="login_text">用户登录</div>
      <login-account
        ref="alogin"
        style="margin-top: 20px"
        @validateFail="validateFail"
        @success="requestSuccess"
        @fail="requestFailed"
      />
      <!--        </a-tab-pane>-->

      <!--        &lt;!&ndash;        <a-tab-pane key="tab2" tab="手机号登录">&ndash;&gt;-->
      <!--        &lt;!&ndash;          <login-phone ref="plogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed"></login-phone>&ndash;&gt;-->
      <!--        &lt;!&ndash;        </a-tab-pane>&ndash;&gt;-->
      <!--      </a-tabs>-->

      <a-form-model-item>
        <!--        <a-checkbox @change="handleRememberMeChange" default-checked>自动登录</a-checkbox>-->
        <!--        <router-link :to="{ name: 'alteration'}" class="forge-password" style="float: right;">-->
        <!--          忘记密码-->
        <!--        </router-link>-->
        <router-link :to="{ name: 'register' }" class="forge-password regist_text"> 注册账户 </router-link>
      </a-form-model-item>
      <a-form-item style="text-align: center">
        <a-button
          size="large"
          type="primary"
          html-type="submit"
          class="login-button"
          :loading="loginBtn"
          :disabled="loginBtn"
          @click.stop.prevent="handleSubmit"
        >
          确&nbsp;&nbsp;定
        </a-button>
      </a-form-item>
    </a-form-model>

    <two-step-captcha
      v-if="requiredTwoStepCaptcha"
      :visible="stepCaptchaVisible"
      @success="stepCaptchaSuccess"
      @cancel="stepCaptchaCancel"
    />
    <login-select-tenant ref="loginSelect" @success="loginSelectOk" />
    <third-login ref="thirdLogin" />
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'

import LoginAccount from './LoginAccount'
// import LoginPhone from './LoginPhone'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    // LoginPhone
  },
  data() {
    return {
      customActiveKey: 'tab1',
      rememberMe: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: '',
        iv: '',
      },
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData()
    this.rememberMe = true
  },
  methods: {
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /** 跳转到登录页面的参数-账号获取*/
    getRouterData() {
      this.$nextTick(() => {
        const temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    // 登录
    handleSubmit() {
      this.loginBtn = true
      if (this.customActiveKey === 'tab1') {
        // 使用账户密码登录
        this.$refs.alogin.handleLogin(this.rememberMe)
      } else {
        // 手机号码登录
        this.$refs.plogin.handleLogin(this.rememberMe)
      }
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false
    },
    // 登录后台成功
    requestSuccess(loginResult) {
      this.$refs.loginSelect.show(loginResult)
    },
    // 登录后台失败
    requestFailed(err) {
      const description = ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试'
      this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4,
      })
      // 账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false
    },
    loginSelectOk() {
      this.loginSuccess()
    },
    // 登录成功
    loginSuccess() {
      this.$router.push({ path: '/dashboard/analysis' }).catch(() => {
        console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`,
      })
    },

    stepCaptchaSuccess() {
      this.loginSuccess()
    },
    stepCaptchaCancel() {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    // 获取密码加密规则
    getEncrypte() {
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING)
      if (encryptedString == null) {
        getEncryptedString().then((data) => {
          this.encryptedString = data
        })
      } else {
        this.encryptedString = encryptedString
      }
    },
  },
}
</script>
<style lang="less" scoped>
.main {
  .user-layout-login {
    border-radius: 10px;
    padding: 10px 90px;
    position: absolute;
    width: 720px;
    border-radius: 25px;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff90;
    box-shadow: 0px 0px 10px #33333350;
    // background: url('~@assets/5.png') no-repeat;
    .login_text {
      -webkit-text-stroke: 1px #ffffff;
      margin-top: 10px;
      font-weight: bold;
      font-size: 35px;
      color: #d82626;
      width: 100%;
      text-align: center;
    }
    .str {
      z-index: 133;
      position: absolute;
      font-size: 30px;
      color: white;
      top: -10px;
      right: 240px;
    }
    .user-login-other {
      text-align: left;
      margin-top: 24px;
      line-height: 22px;
    }

    .login-button {
      width: 200px;
      border: 0;
      background-color: #ae3f3f;
      border-radius: 30px;
      font-size: 18px;
      height: 40px;
      line-height: 40px;
      color: #ffffff;
      // background: url('~@assets/botton.png') no-repeat;
      // background-size: 100% 100%;
      //width: 200px;
      //color: #ffffff;
      //font-weight: bold;
      //background: #ae3f3f;
      //border-radius: 25px;
      //font-size: 20px;
      //margin-top: 20px;
    }

    &::before {
      content: '';
      // background: url('~@assets/3.png') no-repeat;
      background-size: 100% 100%;
      width: 365px;
      height: 65px;
      display: inline-block;
      position: absolute;
      left: 125px;
      top: -19px;
    }
  }
}
.regist_text {
  float: right;
  margin-right: 10px;
  font-weight: bold;
  font-size: 20px;
  color: #d82626;
}
@media only screen and (min-width: 1280px) and (max-width: 1439px) {
  .user-layout-login {
    border-radius: 10px;
    padding: 10px 90px;
    position: absolute;
    width: 720px;
    border-radius: 25px;
    top: 62%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff90;
    box-shadow: 0px 0px 10px #33333350;
  }
}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>
